package cn.dglydrpy.study.j2ee.servlet.day06;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
 * 添加Cookie
 */
public class AddCookieServlet extends HttpServlet{
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//添加cookie,可加多个
		Cookie c1 = new Cookie("username", URLEncoder.encode( "老王", "utf-8"));
		//name为cookie名，value为cookie值，均为String，非ascii码（eg:中文，特殊字符）需进行编码处理
		c1.setMaxAge(40);//设置40s生存时间，服务器转换为过期时间发给浏览器从而控制其到期自动删除，无论浏览器是否关闭该时间内都为其生存期
		response.addCookie(c1);
		Cookie c2 = new Cookie("city", URLEncoder.encode( "北京", "utf-8"));
		response.addCookie(c2);
		Cookie c3 = new Cookie(URLEncoder.encode("字符串","utf-8"),URLEncoder.encode( "1:2;2:2;3:8", "utf-8") );
		response.addCookie(c3);
	}
}
